﻿<template>
  <div class="deviceSlaveTemplatePoints-container">
  
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }"> 
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="从机模板Id">
              <el-select clearable="" filterable="" v-model="queryParams.deviceSlaveTemplateId" placeholder="请选择设备从机模板Id">
                <el-option v-for="(item,index) in deviceSlaveTemplateDeviceSlaveTemplateIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item >
              <el-button-group style="display: flex; align-items: center;">
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'deviceSlaveTemplatePoints:page'"> 查询 </el-button>
                     
              </el-button-group>
            </el-form-item>
            
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 5px">
      <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
                row-key="id"
                @sort-change="sortChange"
				>
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="pointName" label="变量名字"  show-overflow-tooltip="" width="140" />
        <el-table-column prop="dataValue" label="变量值"  show-overflow-tooltip=""  width="100"/>
        <el-table-column prop="updateTime" label="刷新时间"  show-overflow-tooltip="" width="140"/>
          <el-table-column prop="dataType" label="数据类型"  show-overflow-tooltip="" >
            <template #default="scope">
              <el-tag :type="dv('DataType', scope.row.dataType)?.tagType"> {{dv('DataType', scope.row.dataType)?.name}}</el-tag>
            </template>
          </el-table-column>
        <el-table-column prop="reg" label="地址"  show-overflow-tooltip="" />
          <el-table-column prop="regType" label="寄存器类型"  show-overflow-tooltip="" width="140">
            <template #default="scope">
              <el-tag :type="dv('RegType', scope.row.regType)?.tagType"> {{dv('RegType', scope.row.regType)?.name}}</el-tag>
            </template>
          </el-table-column>
    
        <el-table-column prop="cycle" label="采集频率"  show-overflow-tooltip="" />
          <el-table-column prop="precisionEnum" label="小数位数"  show-overflow-tooltip="" >
            <template #default="scope">
              <el-tag :type="dv('PrecisionEnum', scope.row.precisionEnum)?.tagType"> {{dv('PrecisionEnum', scope.row.precisionEnum)?.name}}</el-tag>
            </template>
          </el-table-column>
        <el-table-column label="操作" width="280" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('deviceSlaveTemplatePoints:update') || auth('deviceSlaveTemplatePoints:delete')">
          <template #default="scope">
            <el-button type="primary" style="margin-left:5px;" icon="ele-Top" @click="openReadDeviceSlaveTemplatePoints(scope.row)" v-auth="'deviceTemplateInfo:add'"> 读取 </el-button>
            <el-button v-if="scope.row.regType == 1 || scope.row.regType == 3" type="primary" style="margin-left:5px;" icon="ele-Edit" @click="openEditVariableValue(scope.row)" > 写入 </el-button>
            <!-- v-auth="'deviceTemplateInfo:edit'" -->
            <el-button type="primary" style="margin-left:5px;" icon="ele-Document" @click="openPointList(scope.row)" v-auth="'deviceTemplateInfo:add'"> 历史 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100, 200, 500]"
				size="small"
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
      <printDialog
        ref="printDialogRef"
        :title="printDeviceSlaveTemplatePointsTitle"
        @reloadTable="handleQuery" />
      <editDialog
        ref="editDialogRef"
        :title="editDeviceSlaveTemplatePointsTitle"
        @reloadTable="handleQuery"
      />
      <pointList
        ref="pointListRef"
        title="变量历史"
        @reloadTable="handleQuery"></pointList>
    </el-card>
  </div>
</template>

<script lang="ts" setup="" name="deviceSlaveTemplatePoints">
  import { ref ,onMounted} from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import { auth } from '/@/utils/authFunction';

  import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  import { getDictLabelByVal as dv } from '/@/utils/dict-utils';
  import { formatDate } from '/@/utils/formatTime';


  import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
  import editDialog from '/@/views//Device/Template/deviceSlaveTemplatePoints/component/editDialog.vue'
  import pointList from '/@/views/Device/devicePointsValueHistory/component/pointList.vue'
  import { pageDeviceSlaveTemplatePointsValue ,ReadPointValue,WritePointValue} from '/@/api//Device/Template/deviceSlaveTemplatePointsValue';
	import { getAPI } from '/@/utils/axios-utils';
	import { SysEnumApi } from '/@/api-services/api';
  import commonFunction from '/@/utils/commonFunction';
import { id } from "element-plus/es/locale";

    // 定义 props
    const props = defineProps({
    deviceSlaveTemplateId: {
      type: String,
      default: null
    },
    deviceId: {
    type: String,
    default: null
  }
  });

  const showHasPropsAdvanceQueryUI = ref(true);
  const showAdvanceQueryUI = ref(false);
  const printDialogRef = ref();
  const editDialogRef = ref();
  const pointListRef = ref();
  const loading = ref(false);
  const tableData = ref<any>([]);
  const queryParams = ref<any>({});
  const tableParams = ref({
    page: 1,
    pageSize: 10,
    field: 'createTime', // 默认的排序字段
    order: 'descending', // 排序方向
    descstr: 'descending', // 降序排序的关键字符
    total: 0 as any,
  });

  const printDeviceSlaveTemplatePointsTitle = ref("");
  const editDeviceSlaveTemplatePointsTitle = ref("");

  // 改变高级查询的控件显示状态
  const changeAdvanceQueryUI = () => {
    showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
  }

  // 查询操作
  const handleQuery = async () => {
    console.log("id:"+ props.deviceSlaveTemplateId)
    loading.value = true;
    queryParams.value.deviceSlaveTemplateId = props.deviceSlaveTemplateId;
    var res = await pageDeviceSlaveTemplatePointsValue(Object.assign(queryParams.value, tableParams.value));
    tableData.value = res.data.result?.items ?? [];
    tableParams.value.total = res.data.result?.total;
    loading.value = false;
  };

  // 列排序
  const sortChange = async (column: any) => {
	tableParams.value.field = column.prop;
	tableParams.value.order = column.order;
	await handleQuery();
  };

  // 打开新增页面
  const openAddDeviceSlaveTemplatePoints = () => {
    editDeviceSlaveTemplatePointsTitle.value = '添加从机变量';
    if (props.deviceSlaveTemplateId) {
      editDialogRef.value.openDialog({deviceSlaveTemplateId:props.deviceSlaveTemplateId+""});
    }else{
      editDialogRef.value.openDialog({});
    }

    
  };

  const openReadDeviceSlaveTemplatePoints = async (row: any) => {
    console.log("row:"+row);
    console.log(queryParams.value.deviceId);
    var res = await ReadPointValue({"id":row.id,"deviceId":props.deviceId,"deviceSlaveTemplateId":props.deviceSlaveTemplateId});
    console.log("res:"+res);
    ElMessage.success("读取成功:"+res.data.result);
  };

  const openPointList = (row: any) => {
    pointListRef.value.openDialog(row.id+"");
  };
  // 打开打印页面
  const openPrintDeviceSlaveTemplatePoints = async (row: any) => {
    printDeviceSlaveTemplatePointsTitle.value = '打印从机变量';
  }
  
  // 打开编辑页面
  const openEditDeviceSlaveTemplatePoints = (row: any) => {
    editDeviceSlaveTemplatePointsTitle.value = '编辑从机变量';
    editDialogRef.value.openDialog(row);
  };

  const openEditVariableValue = (row: any) => {
    // 实现修改变量值的逻辑
    ElMessageBox.prompt('请输入新的变量值', '修改变量值', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      inputValue: row.dataValue,
    }).then(({ value }) => {
      // 这里可以调用API更新变量值
      row.dataValue = value;
      ElMessage.success('变量值修改成功');
    }).catch(() => {
      ElMessage.info('已取消修改变量值');
    });
  };

  // 改变页面容量
  const handleSizeChange = (val: number) => {
    tableParams.value.pageSize = val;
    handleQuery();
  };

  // 改变页码序号
  const handleCurrentChange = (val: number) => {
    tableParams.value.page = val;
    handleQuery();
  };

  const deviceSlaveTemplateDeviceSlaveTemplateIdDropdownList = ref<any>([]); 


  onMounted(() => {
    console.log('页面加载时:'+props.deviceSlaveTemplateId);
    console.log('页面加载时deviceId:'+props.deviceId);
    if (props.deviceSlaveTemplateId) {
      queryParams.value.deviceSlaveTemplateId = props.deviceSlaveTemplateId;
      showHasPropsAdvanceQueryUI.value = false;
    }

    if (props.deviceId) {
      queryParams.value.deviceId = props.deviceId;
    }

    handleQuery();

  });
   

</script>
<style scoped>
:deep(.el-input),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}

</style>

